# Maintainer: Konstantin Podsvirov <konstantin@podsvirov.pro>

_realname=mosquitto
pkgbase=mingw-w64-${_realname}
pkgname="${MINGW_PACKAGE_PREFIX}-${_realname}"
pkgver=2.0.16
pkgrel=1
pkgdesc="An Open Source MQTT Broker (mingw-w64)"
arch=('any')
mingw_arch=('mingw32' 'mingw64' 'ucrt64' 'clang64' 'clangarm64' 'clang32')
url="https://mosquitto.org"
license=('EPL' 'EDL')
depends=("${MINGW_PACKAGE_PREFIX}-c-ares"
         "${MINGW_PACKAGE_PREFIX}-cjson"
         "${MINGW_PACKAGE_PREFIX}-libwebsockets"
         "${MINGW_PACKAGE_PREFIX}-libwinpthread"
         "${MINGW_PACKAGE_PREFIX}-openssl")
makedepends=("${MINGW_PACKAGE_PREFIX}-cc"
             "${MINGW_PACKAGE_PREFIX}-cmake"
             "${MINGW_PACKAGE_PREFIX}-ninja"
             "${MINGW_PACKAGE_PREFIX}-winpthreads"
             "${MINGW_PACKAGE_PREFIX}-docbook-xsl")
source=("https://mosquitto.org/files/source/mosquitto-${pkgver}.tar.gz"{,.asc})
sha256sums=('638db9e43e89d243ea98d10d66e76ff376131217780decd01abaffacc66d8035'
            'SKIP')
validpgpkeys=('A0D6EEA1DCAE49A635A3B2F0779B22DFB3E717B7') # Roger A. Light <roger@atchoo.org>

prepare() {
  cd "${srcdir}/${_realname}-${pkgver}"

}

build() {
  mkdir -p "${srcdir}/build-${MSYSTEM}" && cd "${srcdir}/build-${MSYSTEM}"

  declare -a _extra_config
  if check_option "debug" "n"; then
    _extra_config+=("-DCMAKE_BUILD_TYPE=Release")
  else
    _extra_config+=("-DCMAKE_BUILD_TYPE=Debug")
  fi

  MSYS2_ARG_CONV_EXCL="-DCMAKE_INSTALL_PREFIX=" \
  ${MINGW_PREFIX}/bin/cmake \
    -G"Ninja" \
    -DCMAKE_INSTALL_PREFIX=${MINGW_PREFIX} \
    ${_extra_config[@]} \
    -DCMAKE_INSTALL_SBINDIR=bin \
    -DWITH_STATIC_LIBRARIES=ON \
    -DWITH_PIC=ON \
    ../${_realname}-${pkgver}

  ${MINGW_PREFIX}/bin/cmake --build ./
}

check() {
  cd "${srcdir}/build-${MSYSTEM}"

  ${MINGW_PREFIX}/bin/ctest ./ || true
}

package() {
  cd "${srcdir}/build-${MSYSTEM}"

  DESTDIR="${pkgdir}" ${MINGW_PREFIX}/bin/cmake --install ./

  install -Dm644 "${srcdir}/${_realname}-${pkgver}/LICENSE.txt" "${pkgdir}${MINGW_PREFIX}/share/licenses/${_realname}/LICENSE"
}
